package com.springboot.study.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Title:TaskExecutePool
 * @Description:
 * @author:min.luo
 * @date:2020/8/17 20:59
 */
@Slf4j
@Configuration
public class TaskExecutePool {


    @Bean(name = "asyncServiceExecutor")
    public ThreadPoolTaskExecutor taskAsyncPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程池大小
        executor.setCorePoolSize(10);
        //最大线程数
        executor.setMaxPoolSize(100);
        //队列容量
        executor.setQueueCapacity(Integer.MAX_VALUE);
        //活跃时间
        executor.setKeepAliveSeconds(100);
        //线程名字前缀
        executor.setThreadNamePrefix("invoice-");

        // setRejectedExecutionHandler：当pool已经达到max size的时候，如何处理新任务
        // CallerRunsPolicy：不在新线程中执行任务，而是由调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        log.info("executor.getCorePoolSize():"+ executor.getCorePoolSize());
        return executor;
    }


}
